package edu.nf.project.mappers.storeOperation.convention;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.nf.project.entitys.entity.storeOperation.convention.operate.CustomerOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单统计Mapper接口
 */
@Mapper
public interface OrderStatisticsMapper extends BaseMapper<CustomerOrder> {
    
    /**
     * 获取指定时间段内的总营业额
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 总营业额
     */
    @Select("<script>" +
            "SELECT COALESCE(SUM(payable_amount), 0) FROM customer_order " +
            "WHERE order_status = 0 " +
            "<if test='startTime != null'>" +
            "AND place_order_time >= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "AND place_order_time &lt;= #{endTime}" +
            "</if>" +
            "</script>")
    BigDecimal getTotalRevenue(@Param("startTime") LocalDateTime startTime, 
                             @Param("endTime") LocalDateTime endTime);

    /**
     * 获取指定时间段内的总订单数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 总订单数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM customer_order " +
            "WHERE order_status = 0 " +
            "<if test='startTime != null'>" +
            "AND place_order_time >= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "AND place_order_time &lt;= #{endTime}" +
            "</if>" +
            "</script>")
    Long getTotalOrderCount(@Param("startTime") LocalDateTime startTime, 
                          @Param("endTime") LocalDateTime endTime);

    /**
     * 获取指定时间段内的平均客单价
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 平均客单价
     */
    @Select("<script>" +
            "SELECT COALESCE(AVG(total_amount), 0) FROM customer_order " +
            "WHERE order_status = 0 " +
            "<if test='startTime != null'>" +
            "AND place_order_time >= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "AND place_order_time &lt;= #{endTime}" +
            "</if>" +
            "</script>")
    BigDecimal getAverageOrderAmount(@Param("startTime") LocalDateTime startTime, 
                                   @Param("endTime") LocalDateTime endTime);
} 